package com.lm.controller;


import com.lm.model.DpChannel;
import com.lm.service.DpChannelService;
import com.meirengu.common.StatusCode;
import com.meirengu.controller.BaseController;
import com.meirengu.model.Page;
import com.meirengu.model.Result;
import com.meirengu.utils.NumberUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("dpChannel")
public class DpChannelController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(DpChannelController.class);

    @Autowired
    private DpChannelService dpChannelService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public Result listMultiple(@RequestParam(value = "page_num", required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "page_size", required = false, defaultValue = "10") Integer pageSize,
                               @RequestParam(value = "sortby", required = false) String sortBy,
                               @RequestParam(value = "order", required = false) String order,
                               @RequestParam(value = "channel_no", required = false) String channelNo,
                               @RequestParam(value = "channel_name", required = false) String channelName,
                               @RequestParam(value = "staff_account", required = false) String staffAccount,
                               @RequestParam(value = "createDateBegin", required = false) String createDateBegin,
                               @RequestParam(value = "createDateEnd", required = false) String createDateEnd) {
        DpChannel dpChannel = dpChannelService.detailByChannelNo("dp_channel_001");
        try {
            Map<String, Object> paramMap = new HashedMap();
            Page page = super.setPageParams(pageNum, pageSize);
            if (StringUtils.isNotBlank(sortBy)) {
                paramMap.put("sortBy", sortBy);
            }
            if (StringUtils.isNotBlank(order)) {
                paramMap.put("order", order);
            }
            if (StringUtils.isNotBlank(channelNo)) {
                paramMap.put("channelNo", channelNo);
            }
            if (StringUtils.isNotBlank(channelName)) {
                paramMap.put("channelName", channelName);
            }
            if (StringUtils.isNotBlank(staffAccount)) {
                paramMap.put("staffAccount", staffAccount);
            }

            page = dpChannelService.getListByPage(page, paramMap);
            return super.setResult(StatusCode.OK, page, StatusCode.codeMsgMap.get(StatusCode.OK));
        } catch (Exception e) {
            logger.error("throw exception: {}", e);
            e.printStackTrace();
            return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }


    }

    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ResponseBody
    public Result insert(@RequestParam(value = "create_date", required = false) String createDate,
                         @RequestParam(value = "channel_no", required = false) String channelNo,
                         @RequestParam(value = "channel_name", required = false) String channelName,
                         @RequestParam(value = "staff_id", required = false) String staffId,
                         @RequestParam(value = "staff_name", required = false) String staffName,
                         @RequestParam(value = "secret", required = false) String secret,
                         @RequestParam(value = "area_value", required = false) String areaValue) {

        DpChannel dpChannel = new DpChannel();
        try {

            if (StringUtils.isNotBlank(createDate)) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = null;
                date = dateFormat.parse(createDate);
                dpChannel.setCreateDate(date);
            }
            if (StringUtils.isNotBlank(staffId)) {
                dpChannel.setStaffId(staffId);
            }
            if (StringUtils.isNotBlank(channelNo)) {
                dpChannel.setChannelNo(channelNo);
            }
            if (StringUtils.isNotBlank(channelName)) {
                dpChannel.setChannelName(channelName);
            }
            if (StringUtils.isNotBlank(staffName)) {
                dpChannel.setStaffName(staffName);
            }
            if (StringUtils.isNotBlank(secret)) {
                dpChannel.setSecret(secret);
            }
            if (StringUtils.isNotBlank(areaValue)) {
                dpChannel.setAreaValue(areaValue);
            }
            int insertNum = dpChannelService.insert(dpChannel);
            if (insertNum < 1) {
                return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
            }
            return super.setResult(StatusCode.OK, null, StatusCode.codeMsgMap.get(StatusCode.OK));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("throw exception: {}", e);
            e.printStackTrace();
            return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }
    }

    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Result delete(@RequestParam(value = "id", required = false) Integer id) {
        try {
            if (!NumberUtil.isNullOrZero(id)) {
                int deleteNum = dpChannelService.delete(id);
                if (deleteNum > 1) {
                    return super.setResult(StatusCode.OK, null, StatusCode.codeMsgMap.get(StatusCode.OK));
                }
            }
            return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("throw exception: {}", e);
            return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestParam(value = "id", required = false) Integer id,
                         @RequestParam(value = "channel_no", required = false) String channelNo,
                         @RequestParam(value = "channel_name", required = false) String channelName,
                         @RequestParam(value = "staff_id", required = false) String staffId,
                         @RequestParam(value = "staff_name", required = false) String staffName,
                         @RequestParam(value = "secret", required = false) String secret,
                         @RequestParam(value = "area_value", required = false) String areaValue) {

        DpChannel dpChannel = new DpChannel();
        try {

            if (!NumberUtil.isNullOrZero(id)) {
                dpChannel.setId(id);
            }
            if (StringUtils.isNotBlank(staffId)) {
                dpChannel.setStaffId(staffId);
            }
            if (StringUtils.isNotBlank(channelNo)) {
                dpChannel.setChannelNo(channelNo);
            }
            if (StringUtils.isNotBlank(channelName)) {
                dpChannel.setChannelName(channelName);
            }
            if (StringUtils.isNotBlank(staffName)) {
                dpChannel.setStaffName(staffName);
            }
            if (StringUtils.isNotBlank(secret)) {
                dpChannel.setSecret(secret);
            }
            if (StringUtils.isNotBlank(areaValue)) {
                dpChannel.setAreaValue(areaValue);
            }
            int insertNum = dpChannelService.update(dpChannel);
            if (insertNum < 1) {
                return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
            }
            return super.setResult(StatusCode.OK, null, StatusCode.codeMsgMap.get(StatusCode.OK));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("throw exception: {}", e);
            e.printStackTrace();
            return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }
    }

}
